package com.csxy.lsh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.lsh.JwtUtils;
import com.csxy.lsh.MD5;
import com.csxy.lsh.UcenterMember;
import com.csxy.lsh.entity.vo.LoginVo;
import com.csxy.lsh.entity.vo.RegisterVo;
import com.csxy.lsh.exception.MyException;
import com.csxy.lsh.mapper.UcenterMapper;
import com.csxy.lsh.service.UcenterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UcenterServiceImpl extends ServiceImpl<UcenterMapper, UcenterMember> implements UcenterService {
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getPhone();
        String password = loginVo.getPassword();
        //判断手机号是否为空
        if (StringUtils.isEmpty(mobile)&& StringUtils.isEmpty(password)){
            throw new RuntimeException("手机号和密码不能为空");
        }

        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member == null ){
            throw new RuntimeException("手机号不存在");
        }
        //加密算法验证密码
        if (!MD5.encrypt( password).equals(member.getPassword())){
            throw new RuntimeException("密码错误");
        }

        if (member.getIsDisabled()){
            throw new MyException(20001,"账号被禁用");
        }

        //生成token
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;





    }

    @Override
    public String register(RegisterVo registerVo) {

        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();


        if (org.springframework.util.StringUtils.isEmpty(nickname)
                || org.springframework.util.StringUtils.isEmpty(code)
                || org.springframework.util.StringUtils.isEmpty(mobile)
                || org.springframework.util.StringUtils.isEmpty(password)) {
            throw new RuntimeException("注册信息不能为空");
        }
        //判断效验码是否正确
        if (!code.equals(("7777"))) {
            throw new MyException(20001, "验证码错误");
        }

        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count >= 1) {
            throw new MyException(20001, "手机号已存在");


        }
        //添加用户数据到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://edu-online-demo.oss-cn-beijing.aliyuncs.com/2021/05/07/c0d0c0c0-c0d0-c0d0-c0d0-c0d0c0c0c0c0.png");
        baseMapper.insert(member);
        return "";
    }



    @Autowired
    private UcenterMapper ucenterMapper;
    @Override
    public Integer countRegister(String day) {
        return ucenterMapper.getCountRegister(day);
    }

}

